package com.pets.springboot_petshop.controller;

import com.pets.springboot_petshop.entity.Order;
import com.pets.springboot_petshop.entity.Refund;
import com.pets.springboot_petshop.pojo.Result;
import com.pets.springboot_petshop.service.OrderService;
import com.pets.springboot_petshop.service.RefundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: wxk
 * @Email: 2089967794@qq.com
 * @Date: 2024/10/15 下午3:54 周二
 * @Description: 退款控制层
 */
@RestController
@RequestMapping("/Refund")
public class RefundController {
    @Autowired
    private RefundService refundService;
    @Autowired
    private OrderService orderService;
    //id查询
    @GetMapping("select")
    private Result select(@RequestParam int id) {
        Refund refund = refundService.select(id);
        if (refund != null) {
            return Result.success(refund);
        }else {
            return Result.error("获取失败");
        }
    }
    //user_id查询
    @GetMapping("selectuser_id")
    private Result selectuser_id(@RequestParam int user_id) {
        List<Refund> refund = refundService.selectuser_id(user_id);
        if (refund != null) {
            return Result.success(refund);
        }else {
            return Result.error("获取失败");
        }
    }
    //查询所有
    @GetMapping("/findAll")
    private Result findAll() {
        List<Refund> refund = refundService.findAll();
        if(refund == null || refund.isEmpty()){
            return Result.error("获取失败");
        }else {
            return Result.success(refund);
        }
    }
    //添加
    @PostMapping("add")
    private Result add(@RequestBody Refund refund) {
        if (refundService.add(refund)>0){
            return Result.success("新增成功");
        }else {
            return Result.error("获取失败");
        }
    }

    //删除
    @PostMapping("delete")
    private Result delete(@RequestParam Integer id){
        if (refundService.delete(id)>0){
            return Result.success("删除成功");
        }else {
            return Result.error("删除失败");
        }
    }
    //修改数据
    @PostMapping("update")
    private Result update(@RequestBody Refund refund){
        // 首先，尝试更新refund表
        if (refundService.update(refund) > 0){
            // 如果更新成功，尝试更新order表
            Order order = new Order();
            order.setId(refund.getOrder_id());
            order.setStatus(refund.getStatus());
            if (orderService.updatestatus(order) > 0){
                return Result.success("修改成功");
            }else {
                return Result.error("订单状态修改失败");
            }
        }else {
            return Result.error("退款信息修改失败");
        }
    }
}
